<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-row type="flex" class="row-bg" justify="end">
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <el-input
              v-model.number="search"
              size="mini"
              placeholder="输入客户号码或客户姓名搜索"
              @keyup.enter.native="getArchives(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <div class="block">
              <el-date-picker
                v-model="insuranceTime"
                @change="handleAdvancedSearch"
                type="month"
                placeholder="选择投保日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </div>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getArchives(15, 0)"
            >搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-circle-close-outline"
              @click="resetSearch"
            >重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <archivesDialog
      :title="title"
      :editDialog="editDialog"
      :archivesForm="archivesForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></archivesDialog>
    <el-table
      v-loading="loading"
      border
      :row-class-name="tableRowClassName"
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column prop="id" label="序号" width="45" fixed></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="140" fixed>
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD h:mm:ss")}}</template>
      </el-table-column>
      <el-table-column prop="number" label="编号" width="80" fixed></el-table-column>
      <el-table-column prop="phone" label="客户电话" width="100" fixed></el-table-column>
      <!-- <el-table-column prop="idCard" label="身份证号码" width="110" fixed></el-table-column> -->
      <el-table-column prop="name" label="客户姓名" width="70" fixed></el-table-column>
      <!-- <el-table-column prop="complateTime" label="完件日期" width="110" fixed></el-table-column> -->
      <!-- <el-table-column prop="signTime" label="签单日期" width="110" fixed></el-table-column> -->
      <el-table-column prop="insuranceTime" label="投保日期" width="120"></el-table-column>
      <!-- <el-table-column prop="phone1" label="联系方式1" width="110"></el-table-column> -->
      <!-- <el-table-column prop="phone2" label="联系方式2" width="110"></el-table-column> -->
      <!-- <el-table-column prop="promotionName" label="渠道来源" width="70"></el-table-column> -->
      <!-- <el-table-column prop="home_address" label="现居住地址" width="120"></el-table-column> -->
      <!-- <el-table-column prop="register_address" label="上户地址" width="120"></el-table-column> -->
      <el-table-column prop="useArea" label="用车区域" width="120"></el-table-column>
      <!-- <el-table-column prop="licensePlateNumber" label="车牌号" width="120"></el-table-column> -->
      <!-- <el-table-column prop="frameNumber" label="车架号" width="120"></el-table-column> -->
      <el-table-column prop="warrantMan" label="权证专员" width="120"></el-table-column>
      <!-- <el-table-column prop="cartype" label="购买车型" width="110"></el-table-column> -->
      <!-- <el-table-column prop="guidancePrice" label="指导价" width="120"></el-table-column> -->
      <el-table-column prop="insuranceCompany" label="保险公司" width="120"></el-table-column>
      <el-table-column prop="installment" label="还款金额" width="120"></el-table-column>
      <el-table-column prop="installmentTime" label="还款日期" width="120"></el-table-column>
      <!-- <el-table-column prop="loanAmount" label="贷款金额" width="120"></el-table-column> -->
      <!-- <el-table-column prop="loanPeriod" label="贷款期数" width="120"></el-table-column> -->
      <!-- <el-table-column prop="loanCompany" label="贷款机构名称" width="120"></el-table-column> -->
      <el-table-column prop="deliveryVoucher" label="交车凭证" width="70">
        <template slot-scope="scope" v-if="scope.row.deliveryVoucher.length">
          <a :href="'/server/images/' + scope.row.deliveryVoucher" target="_blank">查看</a>
        </template>
      </el-table-column>
      <el-table-column prop="deliveryPhoto" label="交车照片" width="70">
        <template slot-scope="scope" v-if="scope.row.deliveryPhoto.length">
          <a :href="'/server/images/' + scope.row.deliveryPhoto" target="_blank">查看</a>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="centerName" label="中心" width="50"></el-table-column> -->
      <el-table-column prop="deptName" label="部门" width="50"></el-table-column>
      <el-table-column prop="salesmanName" label="业务员" width="70"></el-table-column>
      <!-- <el-table-column prop="remarks" label="备注" width="120"></el-table-column> -->
      <el-table-column prop="operation" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import archivesDialog from "@/components/archives_dialog";
import { getFilterMonthLastDay, getNowTime } from "@/utils/getCurrMonth";

export default {
  name: "archives",
  data() {
    return {
      page: 15,
      insuranceTime: "",
      advancedSearch: ["", ""],
      total: 0,
      search: "",
      currentPage: 1,
      pages: [15, 20, 25, 35],
      title: "",
      tableData: [],
      val: 1,
      loading: true,
      remindTime: 90,
      editDialog: false,
      archivesForm: {}
    };
  },
  created() {
    this.getArchives(15, 0);
  },
  methods: {
    judge(arg) {
      var time = 0;
      let from_y = arg.split("-")[0];
      let to_y = getNowTime().split("-")[0];
      if (from_y === to_y) {
        return false;
      }
      let y = arg.split("-")[0];
      let m = arg.split("-")[1];
      let d = arg.split("-")[2];
      let from = `${to_y}-${m}-${d}`;
      var startTime = new Date(Date.parse(from));
      var endTime = new Date(Date.parse(getNowTime()));
      startTime = startTime.getTime();
      endTime = endTime.getTime();
      time = startTime < endTime ? endTime - startTime : 0;
      const res = Math.floor(time / 86400000) || 1000;
      return res <= this.remindTime ? true : false;
    },
    tableRowClassName({ row, rowIndex }) {
      if (this.judge(row.insuranceTime)) {
        return "warning-row";
      }
      return "";
    },
    handleAdvancedSearch() {
      const time =
        this.insuranceTime &&
        `${this.insuranceTime.split("-")[0]}-${
          this.insuranceTime.split("-")[1]
        }-${getFilterMonthLastDay(
          this.insuranceTime.split("-")[0],
          this.insuranceTime.split("-")[1]
        )}`;
      this.advancedSearch = [];
      this.advancedSearch.push(
        this.insuranceTime ? this.insuranceTime : "",
        time
      );
      this.getArchives(15, 0);
    },
    handleSizeChange(val) {
      this.page = val;
      this.getArchives(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getArchives(this.page, (val - 1) * this.page);
    },
    getArchives(limit, offset) {
      this.$axios
        .get(
          `/api/archives/archives/?limit=${limit}&offset=${offset}&search=${
            this.search
          }&from=${this.advancedSearch[0]}&to=${this.advancedSearch[1]}`
        )
        .then(data => {
          this.tableData = data ? data.doc : [];
          this.total = data.total;
          this.loading = false;
        });
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.delete("/api/archives/removeArchive/" + id).then(data => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.handleUpdate();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetSearch() {
      this.search = "";
      this.getArchives(15, 0);
    },
    hanleClose() {
      this.editDialog = false;
      this.resetForm();
    },
    handleEdit(item) {
      this.editDialog = !this.editDialog;
      this.title = "编辑订单";
      this.archivesForm = item;
    },
    resetForm() {
      this.archivesForm = {
        charge: "",
        cost: ""
      };
    },
    handleUpdate() {
      this.getArchives(this.page, (this.val - 1) * this.page);
    }
  },
  components: {
    archivesDialog
  }
};

</script>
<style>
.el-table .warning-row {
  color: #ff0000;
}
</style>
